/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;

import java.util.Date;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import pojo.Report;
import ulti.HibernateUtil;

/**
 *
 * @author phong
 */
public class ReportDAO {
    //<editor-fold defaultstate="collapsed" desc="Lấy thông tin Report thông qua id">

    public static Report getReportByID(int id) {
        Report Report = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            Report = (Report) session.get(Report.class, id);
        } catch (HibernateException ex) {
            System.err.println(ex);
        }
        return Report;
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="Gui 1 report ve Device">
    public static String doReport(Report report) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction transaction = null;
        try {
            transaction = session.beginTransaction();
            session.save(report);
        } catch (HibernateException ex) {
            transaction.rollback();
            System.err.println(ex);
            return "fail";
        } finally {
            transaction.commit();
            session.close();
        }
        return "success";
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="Lấy danh sach Report cho Admin">
    public static List<Report> getReportByAdmin() {
        List<Report> reports = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            String hql = "from Report r order by r.createDate desc";
            Query query = session.createQuery(hql);
            reports = query.list();
        } catch (HibernateException ex) {
            System.err.println(ex);
        }
        return reports;
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="Lấy danh sach Report cua 1 lab cho Admin">
    public static List<Report> getReportByAdmin(String labID) {
        List<Report> reports = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            String hql = "from Report r where r.device.lab.id = :labID order by r.createDate desc";
            Query query = session.createQuery(hql);
            query.setString("labID", labID);
            reports = query.list();
        } catch (HibernateException ex) {
            System.err.println(ex);
        }
        return reports;
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="Lấy danh sach Report thong qua Status cho Admin">
    public static List<Report> getReportByAdminStatus(String status) {
        List<Report> reports = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            String hql = "from Report r where r.status = :status order by r.createDate desc";
            Query query = session.createQuery(hql);
            query.setString("status", status);
            reports = query.list();
        } catch (HibernateException ex) {
            System.err.println(ex);
        }
        return reports;
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="Lấy danh sach Report cua 1 Lab thong qua Status cho Admin">
    public static List<Report> getReportByAdminStatus(String status, String labID) {
        List<Report> reports = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            String hql = "from Report r where r.status= :status"
                    + " and r.device.lab.id = :labID order by r.createDate desc";
            Query query = session.createQuery(hql);
            query.setString("status", status);
            query.setString("labID", labID);
            reports = query.list();
        } catch (HibernateException ex) {
            System.err.println(ex);
        }
        return reports;
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="Lấy danh sach Report giua 2 ngay cho Admin">
    public static List<Report> getReportByAdmin(Date startDate, Date endDate) {
        List<Report> reports = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            String hql = "from Report r where r.createDate between :startDate and :endDate "
                    + "order by r.createDate desc";
            Query query = session.createQuery(hql);
            query.setDate("startDate", startDate);
            query.setDate("endDate", endDate);
            reports = query.list();
        } catch (HibernateException ex) {
            System.err.println(ex);
        }
        return reports;
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="Lấy danh sach Report giua 2 ngay cua 1 lab cho Admin">
    public static List<Report> getReportByAdmin(Date startDate, Date endDate, String labID) {
        List<Report> reports = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            String hql = "from Report r where r.device.lab.id = :labID "
                    + " where r.createDate between :startDate and :endDate"
                    + " order by r.createDate desc";
            Query query = session.createQuery(hql);
            query.setDate("startDate", startDate);
            query.setDate("endDate", endDate);
            query.setString("labID", labID);
            reports = query.list();
        } catch (HibernateException ex) {
            System.err.println(ex);
        }
        return reports;
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="Lấy danh sach Report thong qua Status cho Admin">
    public static List<Report> getReportByAdminStatus(String status, Date startDate, Date endDate) {
        List<Report> reports = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            String hql = "from Report r where r.status= :status "
                    + " where r.createDate between :startDate and :endDate"
                    + " order by r.createDate desc";
            Query query = session.createQuery(hql);
            query.setString("status", status);
            query.setDate("startDate", startDate);
            query.setDate("endDate", endDate);
            reports = query.list();
        } catch (HibernateException ex) {
            System.err.println(ex);
        }
        return reports;
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="Lấy danh sach Report cua 1 Lab thong qua Status cho Admin">
    public static List<Report> getReportByAdminStatus(String status, Date startDate, Date endDate, String labID) {
        List<Report> reports = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            String hql = "from Report r where r.status= :status"
                    + " where r.createDate between :startDate and :endDate"
                    + " and r.device.lab.id = :labID"
                    + " order by r.createDate desc";
            Query query = session.createQuery(hql);
            query.setString("status", status);
            query.setDate("startDate", startDate);
            query.setDate("endDate", endDate);
            query.setString("labID", labID);
            reports = query.list();
        } catch (HibernateException ex) {
            System.err.println(ex);
        }
        return reports;
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="Lấy danh sach Report cua 1 user">
    public static List<Report> getReportByUser(String user) {
        List<Report> reports = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            String hql = "from Report r where r.userBySender.id = :user order by r.createDate desc";
            Query query = session.createQuery(hql);
            query.setString("user", user);
            reports = query.list();
        } catch (HibernateException ex) {
            System.err.println(ex);
        }
        return reports;
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="Lấy danh sach Report cua 1 Technican">
    public static List<Report> getReportByTechnican() {
        List<Report> reports = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            String hql = "from Report r where r.status = 'In Process'"
                    + " order by r.createDate desc";
            Query query = session.createQuery(hql);
            reports = query.list();
        } catch (HibernateException ex) {
            System.err.println(ex);
        }
        return reports;
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="Lấy danh sach Report can xu ly cua 1 technican">
    public static List<Report> getWorkByTechnican(String technicanID) {
        List<Report> reports = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            String hql = "from Report r where r.userByTechnician = :technicanID"
                    + " order by r.createDate desc";
            Query query = session.createQuery(hql);
            query.setString("technicanID", technicanID);
            reports = query.list();
        } catch (HibernateException ex) {
            System.err.println(ex);
        }
        return reports;
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="Thay doi trang thai cua Report">
    public static String changeStatusReport(Report report) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction transaction = null;
        try {
            transaction = session.beginTransaction();
            session.update(report);
        } catch (HibernateException ex) {
            transaction.rollback();
            System.err.println(ex);
            return "fail";
        } finally {
            transaction.commit();
            session.close();
        }
        return "success";
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="Kiem tra 1 Report da hoan thanh chua">
    public static boolean checkDoneReport(int reportID) {
        List<Report> reports = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            String hql = "from Report r where r.id = :reportID and r.status = 'Done'";
            Query query = session.createQuery(hql);
            query.setInteger("reportID", reportID);
            reports = query.list();
        } catch (HibernateException ex) {
            System.err.println(ex);
        }
        if (reports.size() > 0) {
            return false;
        } else {
            return true;
        }
    }
    //</editor-fold>
}
